GBML manual / 3D

Collision in 3D

How the camera walks without going through walls, and how to ask the map what is ahead.

Walking with collision

view3d_move is the one call that moves the camera and respects the map. You give it how far to go forward and how far to go to the right, in pixels; it moves the camera, and stops it where a wall would be.

The camera is a small square. view3d_radius sets how far from its middle that square reaches, so the camera never touches a wall. The two directions are tried one after the other: if forward is blocked, the sideways part still happens, so the camera slides along walls instead of stopping dead, which is what makes corners feel good to walk round.

view3d_camera is different: it puts the camera exactly where you say, without checking. Use it to set the start, or for a teleport.

Asking what is ahead

view3d_ray sends one ray from the camera along any angle and says how far it goes before a wall. It uses the very same map the picture is made from, so it answers what you would see along that line. Afterwards view3d_hit_x, view3d_hit_y and view3d_hit_wall say where the ray landed and what kind of wall it was.

// Is there a wall within 20 pixels straight ahead?
if (view3d_ray(view3d_angle()) < 20) {
    // too close: turn away
    view3d_turn(5);
}

A ray costs about the same as one column of the picture, so a handful of rays a frame is free.

Lines of sight and shots

view3d_line_clear asks whether anything solid is between two places. view3d_shoot finds the first instance of an object a shot reaches before a wall. Both are in Enemies and items in 3D.

What counts as a wall

A solid tile, a door that is not open, and the edge of the room. A tile the room treats as open is open, whatever picture it shows.

See also: 3D viewMaking 3D wallsEnemies and items in 3Dview3d_moveview3d_radiusview3d_rayview3d_line_clearview3d_shoot